import { Component, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ProcessingContentApplyChangeServiceServiceProxy } from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import { FlowworkauditDetailModelComponent } from '../flowworkauditDetailModel/flowworkauditDetailModel.component';
import { ProcessingOrderListComponent } from '../ProcessingOrderList/ProcessingOrderList.component';
import { ResourceLoader } from '@angular/compiler';

@Component({
    selector: 'ProcessingContentChangeList',
    templateUrl: './ProcessingContentChangeList.component.html',
    styleUrls: ['./ProcessingContentChangeList.component.css']
})
export class ProcessingContentChangeListComponent extends AppComponentBase {


    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;

    filterText = '';
    advancedFiltersAreShown = false;

    startDate: any;
    endDate: any;

    constructor(injector: Injector,
        private _processingContentApplyChangeServiceServiceProxy: ProcessingContentApplyChangeServiceServiceProxy,
        private _filedownloadService: FileDownloadService) {
        super(injector);

    }

    getProcessingContentChangeList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._processingContentApplyChangeServiceServiceProxy.getProcessingContentChangeForList(
            this.startDate,
            this.endDate,
            this.filterText,
            'code',
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event),
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            console.log(result);
            this.primengTableHelper.hideLoadingIndicator();
        });
    }

    exportToexcel(): void {
        this._processingContentApplyChangeServiceServiceProxy.getProcessingContentChangeListForExcel(
            this.filterText,
            this.startDate,
            this.endDate
            ).subscribe(dto => {
            this._filedownloadService.downloadTempFile(dto);
        });
    }
}
